package io.github.biezhi.wechat.api;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @author sunjisong 2019-07-30
 */
public class KeepAliveLoop implements Delayed {

    private long triggerTimeMills;

    public KeepAliveLoop(long triggerTimeMills){
        this.triggerTimeMills = triggerTimeMills;
    }

    public long getTriggerTimeMills() {
        return triggerTimeMills;
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(triggerTimeMills - System.currentTimeMillis(),TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        if(this.getDelay(TimeUnit.MILLISECONDS) < o.getDelay(TimeUnit.MILLISECONDS))
            return -1;
        else if(this.getDelay(TimeUnit.MILLISECONDS) > o.getDelay(TimeUnit.MILLISECONDS))
            return 1;
        else
            return 0;
    }
}
